package com.jic.veteran.controller;

import com.jic.veteran.common.api.CommonPage;
import com.jic.veteran.common.api.CommonResult;
import com.jic.veteran.model.BigScreenTimeInfo;
import com.jic.veteran.service.BigScreenTimeService;
import com.jic.veteran.service.UmsAdminCacheService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@Api(tags = "BigScreenTimeController")
@RequestMapping("/bigScreenTime")
public class BigScreenTimeController {

    @Autowired
    private BigScreenTimeService bigScreenTimeService;

    @ApiOperation("大屏展示时间设置列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<BigScreenTimeInfo>> getList() {
        List<BigScreenTimeInfo> list = bigScreenTimeService.getList();
        return CommonResult.success(CommonPage.restPage(list));
    }

    @ApiOperation("大屏展示时间设置新增")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult add(@RequestBody HashMap map) {
        BigScreenTimeInfo bigScreenTimeInfo = new BigScreenTimeInfo();
        bigScreenTimeInfo.setBigScreenTime((String) map.get("bigScreenTime"));
        bigScreenTimeInfo.setStatus("0");
        int insert = bigScreenTimeService.insert(bigScreenTimeInfo);
        if (insert > 0) {
            return CommonResult.success(insert);
        }
        return CommonResult.failed("新增失败");
    }

    @ApiOperation("大屏展示时间设置启用")
    @RequestMapping(value = "/updateStatus", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult updateStatus(@RequestBody HashMap map) {
        Integer id = (Integer) map.get("id");
        String status = (String) map.get("status");
        int updateStatus = bigScreenTimeService.updateStatus(Long.valueOf(id), status);
        if (updateStatus > 0) {
            return CommonResult.success(updateStatus);
        }
        return CommonResult.failed("启用失败");
    }

    @ApiOperation("大屏展示时间设置修改")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@RequestBody HashMap map) {
        Integer id = (Integer) map.get("id");
        String bigScreenTime = (String) map.get("bigScreenTime");
        int status = bigScreenTimeService.update(Long.valueOf(id), bigScreenTime);
        if (status == -1) {
            return CommonResult.failed("只允许一个被启用!");
        }
        if (status > 0) {
            return CommonResult.success(status);
        }
        return CommonResult.failed("修改失败");
    }

    @ApiOperation("大屏展示时间设置删除")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult delete(@RequestBody HashMap map) {
        Integer id = (Integer) map.get("id");
        int delete = bigScreenTimeService.delete(Long.valueOf(id));
        if (delete > 0) {
            return CommonResult.success(delete);
        }
        if (delete == -1) {
            return CommonResult.failed("已启用的不能被删除");
        }
        return CommonResult.failed("删除失败");
    }
}
